package com.tycmc.signelectroniccontracts.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.net.NetUrl;
import com.tycmc.signelectroniccontracts.tools.net.NetWorkUtils;
import com.tycmc.signelectroniccontracts.tools.net.RequestAction;
import com.tycmc.signelectroniccontracts.tools.net.result.NetworkResult;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.show.ProDialog;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.tools.store.SharePre;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;

import java.util.Map;


/**
 * Created by gy on 2017/11/7.
 */

public class PopYanZhengma {

    private Dialog dialog;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            handler.sendEmptyMessageDelayed(msg.what - 1, 1000);//设置1s延迟
            setgetCode(msg.what);
            return false;
        }
    });
    private TextView tvGetCode;
    private ActBase context;
    private String code = "";

    public PopYanZhengma(final ActBase context, final YanZhengMaInter linster, final String contractNo, final String subContractNo) {
        this.context = context;
        View inflate = ((LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.pop_yanzhengma, null);
        String string = "";
        if (SharePre.getString(SharePreName.modile, "").length() > 7) {
            string = SharePre.getString(SharePreName.modile, "").substring(0, 3) + "****" + SharePre.getString(SharePreName.modile, "").substring(SharePre.getString(SharePreName.modile, "").length() - 4);
        } else {
            string = "1***1";
        }
        ((TextView) inflate.findViewById(R.id.tv_phone_number)).setText(string);
        tvGetCode = ((TextView) inflate.findViewById(R.id.tv_getcode));
        tvGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestAction action = new RequestAction(NetUrl.getVerificationCode);
                action.putBody("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo, ""));
                action.putBody("verificationCodeType", "2");
                action.putBody("contractNo", contractNo);
                action.putBody("subContractNo", subContractNo);
                final ProDialog dialog = new ProDialog(context, "");
                dialog.show();
                new NetWorkUtils(context).postJson(action, new NetworkResult() {
                    @Override
                    public void sucess(Map<String, Object> data, String str) {
                        dialog.dismiss();
                        handler.sendEmptyMessage(60);
                        code = "123456";
                    }

                    @Override
                    public void failed(Object data) {
                        dialog.dismiss();
                        handler.sendEmptyMessage(10);
                        code = "1234";
                    }
                });

//                handler.sendEmptyMessage(60);
                v.setEnabled(false);
            }
        });
        final TextView textView = (TextView) inflate.findViewById(R.id.et_code);
        inflate.findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = textView.getText().toString().trim();
                if (TextShow.isEmpty(trim)) {
                    ToastShow.popupToast(context.getString(R.string.please_input_yanzhengma));
                    return;
                }
                if (trim.equals(code)) {
                    if (linster != null) {
                        linster.codeShow(trim);
                    }
                    dialog.dismiss();
                } else {
                    ToastShow.popupToast("请输入正确的验证码");
                }


            }
        });
        inflate.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(inflate);
        // 设置SelectPicPopupWindow弹出窗体的宽
        WindowManager windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        //调整dialog背景大小
        inflate.setLayoutParams(new FrameLayout.LayoutParams((int) ((width < height ? width : height) * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
//                context.hideSoftInputView();
                try {
                    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                } catch (Exception e) {

                }

                if (handler != null) {
                    handler.removeCallbacksAndMessages(null);
                }
            }
        });

    }

    public interface YanZhengMaInter {
        void codeShow(String code);
    }

    private void setgetCode(int what) {
        if (what > 0) {
            tvGetCode.setText(String.format(context.getString(R.string.replay_getcode), what + ""));
            tvGetCode.setEnabled(false);
        } else {
            tvGetCode.setEnabled(true);
            tvGetCode.setText(R.string.repleycode);
            handler.removeCallbacksAndMessages(null);
        }
    }

    public void show(View view) {
        dialog.show();
//        dialog.showAtLocation(view,
//                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}
